package application;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.*;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;

public class Window {
	public static final int WINDOW_WIDTH	=	1280;
	public static final int WINDOW_HEIGHT	=	720;
	public static final String WINDOW_TITLE =	"Arkanoid-Coop by BaciL";
	
	private long time;
	
	public Window(){
		init();
	}
	
	public void init(){
		try{
			Display.setDisplayMode(new DisplayMode(WINDOW_WIDTH, WINDOW_HEIGHT));
			Display.setTitle(WINDOW_TITLE);
			
			PixelFormat pixelFormat = new PixelFormat();
			ContextAttribs contextAtrributes = new ContextAttribs(2, 0);
			Display.create(pixelFormat, contextAtrributes);
			
			glViewport(0, 0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight());
			glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
			glOrtho(0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight(), 0, 1, -1);	// coordinates in pixels
			
			// Enable blending (using alpha channel)
        	glEnable(GL_BLEND);
        	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        	
			//Creating mouse and keyboard
			Keyboard.create();
			Mouse.create();
		}
		catch(LWJGLException lwjglException){
			throw new RuntimeException("[!] Couldn't initialize LWJGL!", lwjglException);
		}
	}
	
	public void clear(){
		glClear(GL_COLOR_BUFFER_BIT);
	}
	
	public void update(){
		Display.update();
	}
	
	public void enableTexturing(){
		glEnable(GL_TEXTURE_2D);
	}
	
	public void disableTexturing(){
		glDisable(GL_TEXTURE_2D);
	}
	
	public void enableTextureSmoothing(){
    	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	}
	
	public void disableTextureSmoothing(){
    	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	}
	
	public void pushTime(){
		time = System.currentTimeMillis();
	}
	
	public long popTime(){
		return time;
	}
}
